<template>
  <div class="container">
    <el-tabs v-model="activeName" @tab-click="handleClick" stretch="true">
      <el-tab-pane label="关注" name="likelist">
        <div class="list" v-for="item in list.likeList" :key="item.id">
          <div class="list-box" @click.stop="toUserInfo(item.id)">
            <el-avatar :size="50" :src="item.headImage"></el-avatar>
            <div class="list-right">
              {{item.nickName.length>10?item.nickName.substring(0,10)+"...":item.nickName}}
              <div class="list-btn" v-show="currentUserId==userId">
                <el-button type="danger" @click.stop="deleteFan(item)">取关-</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="粉丝" name="fanlist">
        <div class="list" v-for="item in list.fanList" :key="item.id">
          <div class="list-box" @click.stop="toUserInfo(item.id)">
            <el-avatar :size="50" :src="item.headImage"></el-avatar>
            <div class="list-right">
              {{item.nickName.length>10?item.nickName.substring(0,10)+"...":item.nickName}}

            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { ElMessage, ElMessageBox } from 'element-plus';
import { userId } from '@/base/index';
export default {
  data () {
    return {
      activeName: "likelist",
      list: {},
      userId: null,
      currentUserId: userId,
    }
  },
  mounted () {
    this.userId = this.$route.params.userId;
    this.getLikeFanList();
  },
  methods: {
    handleClick (tab, event) {
      console.log(tab, event);
    },
    getLikeFanList () {
      this.$axios.get("/fan/list/" + this.userId).then(res => {
        if (res.flag) {
          this.list = res.data;
        }
      })
    },
    //取消关注
    deleteFan (user) {
      ElMessageBox.confirm(
        '确定要取关吗?',
        '取消关注',
        {
          confirmButtonText: '确认',
          cancelButtonText: '舍不得',
          type: 'warning',
        }
      )
        .then(() => {
          //取消
          this.$axios.delete("/fan/" + user.id).then((res) => {
            if (res.flag) {
              this.list.likeList = this.list.likeList.filter(item => item != user)
              ElMessage({
                message: "res.msg",
                grouping: true,
                type: 'success',
                duration: 2000,
              })
            }
          })
        })
        .catch(() => {
        });
    },
    toUserInfo (userId) {
      this.$router.push('/user/info/' + userId);
    }
  }
}
</script>

<style scoped>
.list-box {
  padding: 5px;
  height: 50px;
  margin-bottom: 5px;
  width: 100%;

  position: relative;
}
.list-right {
  width: calc(100% - 70px);
  height: 60px;
  left: 65px;

  position: absolute;
  z-index: 1;
  top: 0;
  line-height: 60px;
  font-size: 16px;
}
.list-btn {
  z-index: 2;
  width: 100px;
  height: 30px;
  position: absolute;
  top: 0;
  right: 0;
}
</style>